<template>
	<view class="root">
		<view class="header">
			<view class="header_top">
				<image @click="back()" src="../../static/img/arrow-left-bold.png" mode=""></image>
				<text>个人信息</text>
			</view>
		</view>

		<view class="body">
			<view v-for="n in list" :key='n.item' class="item">
				<view class="row" @tap="fun(n)">
					<text>{{n.title}}</text>

					<view class="con">
						<text class="text_right">{{n.content}}</text>
						<image class="image_right" src="../../static/img/arrow-right.png" mode=""></image>
					</view>
				</view>
			</view>


		</view>
		<button @click="back_login()" type="default">退出登录</button>
		<view class="content">
			<chunLei-modal v-model="value" :mData="inputData" type="input" @onConfirm="onConfirm" @cancel="cancel" :maskEnable="fa"
			 navMask>
			</chunLei-modal>
		</view>
	</view>
</template>

<script>
	var req = require("../../static/request.js")
	export default {
		data() {
			return {
				value: false,
				fa: false,
				type: 'text',
				this_item: 1,
				stu_id: '',
				user: {
					name: '',
					stu_id: '',
					xueyuan: '',
					classname: '',

				},
				list: [{
						item: 1,
						title: '姓名',
						content: '请完善',
						type: 'text',
						key: 'name'
					}, {
						item: 2,
						title: '学号',
						content: '请完善',
						type: 'number',
						key: 'stu_id'
					},
					{
						item: 3,
						title: '学院',
						content: '请完善',
						type: 'text',
						key: 'xueyuan'
					},
					{
						item: 4,
						title: '专业',
						content: '请完善',
						type: 'text',
						key: 'major'
					},
					{
						item: 5,
						title: '班级',
						content: '请完善',
						type: 'text',
						key: 'class'
					},
					{
						item: 6,
						title: '身份证号',
						content: '请完善',
						type: 'number',
						key: 'id'
					},
					{
						item: 7,
						title: '政治面貌',
						content: '请完善',
						type: 'text',
						key: 'political'
					}
				],

				inputData: {
					title: '修改信息',
					content: [{
						title: '',
						content: '',
						type: 'text',
						placeholder: ''
					}, ]
				}
			}
		},
		methods: {
			// 列表项被点击
			fun: function(res) {
				if (res.item != 1 && res.item != 2) {
					this.this_item = res.item;
					this.inputData.content[0].content = '';
					this.value = !this.value;
					this.inputData.content[0].title = res.title;
					this.inputData.content[0].placeholder = '请输入' + res.title;
					this.inputData.content[0].type = res.type;
				} else if (res.item == 1) {
					uni.showToast({
						title: '姓名暂不允许自行更改,请联系管理员',
						icon: 'none'
					})
				} else {
					uni.showToast({
						title: '学号暂不允许自行更改,请联系管理员',
						icon: 'none'
					})
				}
				// this.type = res.type;

			},
			// 按下对话框确认键
			onConfirm() {
				var _this = this;
				console.log(this.this_item)
				// 对政治面貌单独判断
				if (this.this_item == 7) {
					if (this.inputData.content[0].content == '共青团员' || this.inputData.content[0].content == '中共党员' || this.inputData.content[
							0].content == '群众') {

						uni.getStorage({
							key: 'login',
							success: function(res) {
								req.post('/cgi-bin/user.py', 'cg=g&stu_id=' + _this.stu_id + '&item=' + _this.this_item + '&psw=' + res.data
									.password +
									'&content=' + _this.inputData.content[0].content,
									function(res2) {
										res2 = JSON.parse(res2)
										if (res2.msg == 'true') {

											_this.list[_this.this_item - 1].content = _this.inputData.content[0].content;
											uni.showToast({
												title: '修改成功',
												icon: 'none'
											})
											uni.reLaunch({
												url: ''
											})
										} else {
											uni.showToast({
												title: '失败',
												icon: 'none'
											})
										}
									})
							}
						})

					} else {

						uni.showToast({
							title: '填写错误，取值必须为：共青团员、中共党员、群众',
							icon: 'none'
						})

					}
					// 对身份证进行加密处理
				} else if (this.this_item == 6) {
					console.log('身份证')
				} else {
					if (this.inputData.content[0].content != '') {

						uni.getStorage({
							key: 'login',
							success: function(res) {
								req.post('/cgi-bin/user.py', 'cg=g&stu_id=' + _this.stu_id + '&item=' + _this.this_item + '&psw=' + res.data
									.password +
									'&content=' + _this.inputData.content[0].content,
									function(res2) {
										res2 = JSON.parse(res2)
										if (res2.msg == 'true') {
											_this.list[_this.this_item - 1].content = _this.inputData.content[0].content;
											uni.showToast({
												title: '修改成功',
												icon: 'none'
											})
											// uni.reLaunch({
											// 	url: ''
											// })
										} else {
											uni.showToast({
												title: '失败',
												icon: 'none'
											})
										}
									})
							}
						})

					} else {
						uni.showToast({
							title: '输入为空',
							icon: 'none'
						})
					}
				}

				// var _this = this;
				// uni.getStorage({
				// 	key:'login',
				// 	success:function(res){
				// 		var stu_id = res.data.user


				// 	}
				// })



			},
			cancel() {

			},
			// 返回
			back() {
				uni.navigateBack({
					
				})


			},
			back_login() {
				uni.showModal({
					title: '提示',
					content: '确认退出？',
					success: function(res) {
						if (res.confirm) {
							uni.removeStorage({
								key: 'login',
								success: function() {
									uni.reLaunch({
										url: "../login/login"
									});
								}
							});
						}
					}
				})

			}
		},

		// 载入
		onLoad: function(op) {
			var _this = this;
			try {
				op = JSON.parse(decodeURIComponent(op.content))
				uni.getStorage({
					key: 'login',
					success: function(res) {
						for (var key in op) {
							for (var i = 0; i < _this.list.length; i++) {
								if (key == _this.list[i].key) {
									if (op[key] != '') {
										_this.list[i].content = op[key]
									}

								}
							}
						}

						// _this.list[0].content = op[2];
						// _this.list[1].content = op.stu_id;
						// _this.list[2].content = op.xueyuan;
						// _this.list[3].content = op.major;
						// _this.list[4].content = op.class;
						// _this.list[5].content = op.id;
						_this.stu_id = res.data.user
					},
					fail: function() {
						uni.showToast({
							title: '请先登录',
							duration: 3000,
							icon: 'none'
						});
						// uni.reLaunch({
						// 	//跳转
						// 	url: '../login/login'
						// })
					}
				})
			} catch (e) {
				//TODO handle the exception
				uni.getStorage({
					key: 'login',
					success: function(res) {
						_this.list[0].content = res.data.name
						_this.list[1].content = res.data.user
						_this.stu_id = res.data.user
					},
					fail: function() {
						uni.showToast({
							title: '请先登录',
							icon: 'none'
						});
						// uni.reLaunch({
						// 	//跳转
						// 	url: '../login/login'
						// })
					}

				})
			}



			// req.post('/cgi-bin/user.py','stu',function(res){

			// })
		}
	}
</script>

<style>
	@import url("./user.css");
</style>
